define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'service/agent/index' + location.search,
                    add_url: 'service/agent/add',
                    edit_url: 'service/agent/edit',
                    del_url: 'service/agent/del',
                    multi_url: 'service/agent/multi',
                    import_url: 'service/agent/import',
                    table: 'service_agent',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='type']", form).addClass("selectpage").data("source", "service/agent_user/agentType")
                  .data("primaryKey", "id").data("field", "agent.type");
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: '类型',formatter:function (value,item,index) {
                                if(value ==1){
                                    return '代理商';
                                }else if(value ==2){
                                    return '服务中心';
                                }else if(value ==3){
                                    return '服务站';
                                }
                            }},
                        {field: 'pid', title: __('Pid'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                      {
                        field: 'operate',
                        title:
                          __('Operate'),
                        table:
                        table,
                        events:
                        Table.api.events.operate,
                        formatter:
                        Table.api.formatter.operate,
                        buttons:
                          [
                            {
                              name: 'goods',
                              title: "认证产品",
                              classname: 'btn btn-xs btn-primary btn-dialog',
                              icon: 'fa fa-leaf',
                              url: 'product/goodsselect/index?agent_id={id}',
                              callback: function (data) {
                                // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                              }
                            }
                          ]
                      }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            Controller.api.fieldsBindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            Controller.api.fieldsBindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            fieldsBindevent: function() {
                $(document).on("fa.event.appendfieldlist", '.btn-append', function(e, obj){
                    //通用的表单组件事件绑定和组件渲染
                    Form.events.cxselect(obj);
                });
            }
        }
    };
    return Controller;
});
